var currentHostUrl =  window.location.protocol + "//" + window.location.host + "/";
// var apiHostUrl = "http://127.0.0.1:8020/"
var apiHostUrl = "http://api.junxtec.com/"

$(document).ready(function () {
    layui.use(['element', 'layer'], function () {
    	var element = layui.element;
    	var layer = layui.layer;
    });
});

function login() {

	// console.log(currentHostUrl);

	var username = $("#username").val();
	var password = $("#password").val();

	$.ajax({
		url: apiHostUrl + "login",
		type: "POST",
		dataType: "json",
		contentType : "application/json", 
		data: JSON.stringify({
			"username": username,
			"password": password
		}),
		success: function(result) {
			// console.log(result);
			if(result.errorCode.toString() === "0") {
				// 登录成功
				$.cookie("userId", result.returnData.userId, {expires: 1});
				$.cookie("token", result.returnData.token, {expires: 1});
				location.href = currentHostUrl + "admin/index.html";
			}
			else {
				layer.msg(result.errorCode + ":" + result.errorMsg);
			}
		},
		error: function(result) {
            // console.log(result);
			if (result.errorCode == null || result.errorMsg == null) {
                layer.msg("网络连接失败");
			}
			else {
                layer.msg(result.errorCode + ":" + result.errorMsg);
			}
		}
	});

}